package com.zwps.biz.domain.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwps.biz.api.model.vo.structure.ZManageProtectStruVO;
import com.zwps.biz.api.model.vo.structure.ZManageProtectVO;
import com.zwps.biz.dal.db.dao.ZManageProtectDAO;
import com.zwps.biz.dal.db.object.ZManageProtectDO;
import com.zwps.biz.domain.service.ZManageProtectService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 *
 */
@DS("lhk")
@Service
public class ZManageProtectServiceImpl extends ServiceImpl<ZManageProtectDAO, ZManageProtectDO>
        implements ZManageProtectService {

    @Override
    public List<ZManageProtectStruVO> getManageProtect() {
        List<ZManageProtectStruVO> manageProtectList = new ArrayList<>();
        List<String> companyList = this.list()
                .stream()
                .map(ZManageProtectDO::getManageProtectCompany)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        companyList.stream().forEach(company -> {
            ZManageProtectStruVO protectStruVO = new ZManageProtectStruVO();
            protectStruVO.setManageProtectCompany(company);
            List<ZManageProtectVO> departmentList = this.baseMapper.selectList(
                    new QueryWrapper<ZManageProtectDO>().eq("manage_protect_company", company))
                    .stream()
                    .map(departmentDO -> {
                        ZManageProtectVO manageProtectVO = new ZManageProtectVO();
                        BeanUtil.copyProperties(departmentDO, manageProtectVO);
                        return manageProtectVO;
                    })
                    .collect(Collectors.toList());
            protectStruVO.setList(departmentList);
            manageProtectList.add(protectStruVO);
        });
        return manageProtectList;
    }
}




